<?php

// PDO预处理语句
header('Content-type:text/html;charset=utf8');
$config = [
    'host' => '127.0.0.1',
    'user' => 'root',
    'password' => 'root',
    'database' => 'php8_rainy',
    'charset' => 'utf8'
];
$dsn = sprintf("mysql:host=%s;dbname=%s;charset=%s"
    , $config['host'], $config['database'], $config['charset']);

try {
    $pdo = new PDO($dsn, $config['user'], $config['password']);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    // $sql = "SELECT * FROM news WHERE id =:id";
    $sql = "SELECT * FROM news WHERE id =?";
    $statement = $pdo->prepare($sql);
    // $statement->execute([':id' => $_GET['id']]);
    $statement->execute([$_GET['id']]);
    $data = $statement->fetchAll();
    print_r($data);
} catch (PDOException $e) {
    echo $e->getMessage();
}